/**
 * @file DBMgr.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 数据库管理器
 * @version 0.0.1
 * @date 2020-07-18
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include "DBMgr.hpp"
#include "STL_Mutex.hpp"
#include "STL_Log.hpp"

int DBMgr::s_id = 0;

DBMgr *DBMgr::instance()
{
    static DBMgr _ins;
    return &_ins;
}

DBMgr::DBMgr(/* args */)
{
}

DBMgr::~DBMgr()
{
}

int DBMgr::addDevice(DBMgr::Device info)
{
    int id = DBMgr::s_id++;
    stl::Guard g(m_mtx);
    m_list[id] = info;
    return id;
}
void DBMgr::delDevice(int deviceId)
{
    stl::Guard g(m_mtx);
    std::map<int, DBMgr::Device>::iterator it = m_list.find(deviceId);
    if (it != m_list.end())
        m_list.erase(it);
}
void DBMgr::setDevice(int deviceId, DBMgr::Device info)
{
    stl::Guard g(m_mtx);
    std::map<int, DBMgr::Device>::iterator it = m_list.find(deviceId);
    if (it != m_list.end())
        m_list[deviceId] = info;
}
DBMgr::Device DBMgr::getDevice(int deviceId)
{
    stl::Guard g(m_mtx);
    std::map<int, DBMgr::Device>::iterator it = m_list.find(deviceId);
    if (it != m_list.end())
    {
        return m_list[deviceId];
    }
    else
    {
        return DBMgr::Device();
    }
}
std::map<int, DBMgr::Device> DBMgr::getDeviceList()
{
    stl::Guard g(m_mtx);
    return m_list;
}

void DBMgr::addTask(int deviceId)
{
    stl::Guard g(m_mtx);
    auto it = m_task.find(deviceId);
    if (it == m_task.end())
    {
        stl::debug("ADD Task[%d]", deviceId);
        m_task[deviceId] = DBMgr::TaskStatus::NEW;
    }
}

void DBMgr::setTask(int deviceId, DBMgr::TaskStatus status)
{
    stl::Guard g(m_mtx);
    auto it = m_task.find(deviceId);
    if (it != m_task.end())
        m_task[deviceId] = status;
}

void DBMgr::delTask(int deviceId)
{
    stl::Guard g(m_mtx);
    auto it = m_task.find(deviceId);
}

std::map<int, DBMgr::TaskStatus> DBMgr::getTaskList()
{
    stl::Guard g(m_mtx);
    return m_task;
}
